import openpyxl


class ReadExcel(object):
    def __init__(self, file, sheet):
        self.file = file
        self.sheet = sheet

    def open_excel(self):
        # 获取工作簿对象
        self.wb = openpyxl.load_workbook(self.file)
        # 选择表单
        self.sh = self.wb[self.sheet]

    def read_data(self):
        """读数据"""
        self.open_excel()
        datas = list(self.sh.rows)
        key = [i.value for i in datas[0]]
        cases = []
        for i in datas[1:]:  # 从第二行开始遍历所有数据
            value = [c.value for c in i]
            cases.append(dict(zip(key, value)))
        return cases

    def write_data(self, row, column, value):
        """写数据"""
        self.open_excel()
        self.sh.cell(row=row, column=column, value=value)
        self.wb.save(self.file)
